﻿using System;
using System.IO;

namespace Ooaavee.Scripting
{
    /// <summary>
    /// Script output files at the file system. 
    /// This holds information about the script's output assembly file.
    /// </summary>
    internal sealed class ScriptOutput
    {
        /// <summary>
        /// No public constructor available, use NewScriptOutput method.
        /// </summary>
        private ScriptOutput() { }

        /// <summary>
        /// A script output directory.
        /// </summary>
        internal DirectoryInfo Directory { get; set; }

        /// <summary>
        /// An assembly file.
        /// </summary>
        internal FileInfo AssemblyFile { get; set; }

        /// <summary>
        /// Creates a new instance of <see cref="ScriptOutput"/> class.
        /// </summary>
        /// <param name="workingDirectory">A working directory for scripts, see <see cref="ScriptingContext.WorkingDirectory"/>.</param>
        /// <returns>script output</returns>
        internal static ScriptOutput NewScriptOutput(DirectoryInfo workingDirectory)
        {
            if (workingDirectory.Exists == false)
            {
                throw new DirectoryNotFoundException(string.Format("A working directory '{0}' does not exist.", workingDirectory.FullName));
            }

            ScriptOutput output = new ScriptOutput();

            output.Directory = workingDirectory.CreateSubdirectory(Guid.NewGuid().ToString());

            output.AssemblyFile = new FileInfo(string.Format(@"{0}\{1}.dll", output.Directory.FullName, Guid.NewGuid()));

            return output;
        }

        /// <summary>
        /// Deletes the content of this script output.
        /// </summary>
        /// <remarks>This method does not throw an exception in any case.</remarks>
        internal void Delete()
        {
            try
            {             
                Directory.Delete(true);
            } 
            catch (Exception ex)
            {
                // No need to handle any exceptions.       
            }
        }
    }
}